<template>
<div class="updatename-container">
    <van-nav-bar title="设置昵称" left-text="取消" right-text="完成" left-arrow @click-left="$emit('close')" @click-right="save" />
    <div class="field-wrap">
        <van-field v-model="message" rows="2" autosize type="textarea" maxlength="7" placeholder="请输入留言" show-word-limit @input="$emit('input',message)" />
    </div>
</div>
</template>

<script>
import { updateUserProfile } from '@/api/user'
export default {
  name: 'UpdateName',
  props: {
    value: {
      type: String,
      required: true
    }
  },
  data () {
    return {
      message: this.value
    }
  },
  methods: {
    async save () {
      this.$toast.loading({
        message: '更新中....',
        overlay: true
      })
      try {
        await updateUserProfile({
          name: this.message
        })
        this.$toast.success('更新昵称成功')
        // 关闭弹出层
        this.$emit('close')
      } catch (err) {
        this.$toast.fail('更新昵称失败')
      }
    }
  }
}
</script>

<style lang="less" scoped>
.van-nav-bar {
    background-color: #fff !important;

    /deep/ .van-nav-bar__title {
        color: #323233;
    }
}

.updatename-container {
    background-color: #f5f7f9;
    height: 100%;
}

.field-wrap {
    padding: 20px;
}

/deep/ .van-field__word-num {
    color: red;
}
</style>
